<!--
 * @Descripttion: 
 * @Author: Silence
 * @Date: 2023-06-03 20:33:39
 * @LastEditors: Silence
 * @LastEditTime: 2023-06-05 22:53:26
-->
<template>
  <div>
    <div
      class="header"
      style="display: flex; justify-content: space-between; padding: 10px"
    >
      <span>共 {{ total }} 条记录</span>
      <el-button
        icon="el-icon-plus"
        size="small"
        type="primary"
        round
        @click="drawerHandle('add')"
      >
        添加面经
      </el-button>
    </div>
    <el-table :data="list" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"
        ><template slot-scope="{ $index }">{{
          fromdata.pageSize * (fromdata.current - 1) + $index + 1
        }}</template>
      </el-table-column>
      <el-table-column prop="stem" label="标题" width="400"> </el-table-column>
      <el-table-column prop="creator" label="作者" width="150">
      </el-table-column>
      <el-table-column prop="likecount" label="点赞" width="100">
      </el-table-column>
      <el-table-column prop="views" label="浏览数" width="100">
      </el-table-column>
      <el-table-column prop="createdAt" label="更新时间" width="200">
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="{ row }">
          <div class="action">
            <i class="el-icon-view" @click="drawerHandle('preview')"></i>
            <i class="el-icon-edit-outline" @click="drawerHandle('edit')"></i>
            <i class="el-icon-delete" @click="handleDelete(row.id)"></i>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="fromdata.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="fromdata.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-drawer :title="titleType" :visible.sync="drawer">
      <el-form label-width="80px">
        <el-form-item label="标题">
          <el-input placeholder="输入面经标题"></el-input>
        </el-form-item>
        <el-form-item label="内容">富文本编辑器</el-form-item>
        <el-form-item>
          <el-button type="primary">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { GetListData, DelListData } from '@/api/article.js'
export default {
  name: 'ArticleIndex',
  data () {
    return {
      list: [],
      title: '',
      drawer: false,
      total: 0,
      fromdata: {
        current: 1,
        pageSize: 10
      }
    }
  },
  created () {
    this.render()
  },
  computed: {
    titleType () {
      if (this.title == 'add') {
        return '添加文章'
      } else if (this.title == 'edit') {
        return '编辑文章'
      } else {
        return '查看文章'
      }
    }
  },
  methods: {
    async render () {
      const res = await GetListData(this.fromdata)
      // console.log(res, 1212)
      this.list = res.data.rows
      this.total = res.data.total
    },
    handleDelete (row) {
      console.log(row, 121)
      DelListData(row)
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.fromdata.pageSize = val
      this.render()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.fromdata.current = val
      this.render()
    },
    drawerHandle (type) {
      console.log(type, 99)
      this.title = type
      this.drawer = true
    }
  },
}
</script>

<style lang="scss">
.action {
  i {
    margin: 0 10px;
  }
}
</style>

